#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "Include/common_pub.h"
#include "Include/student_pub.h"
#include "Include/course_pub.h"
#include "Include/io_pub.h"
#include "globals.h"

void log_message(LogLevel level, const char* format, ...) {
    const char* level_str;
    switch (level) {
        case LOG_INFO: level_str = "INFO"; break;
        case LOG_WARNING: level_str = "WARN"; break;
        case LOG_ERROR: level_str = "ERROR"; break;
        default: level_str = "INFO";
    }
    
    printf("[%s] ", level_str);
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    printf("\n");
}

void student_management() {
    int choice;
    char id[20], name[50];
    
    while(1) {
        printf("\n--- 学生管理 ---\n");
        printf("1. 添加学生\n");
        printf("2. 删除学生\n");
        printf("3. 修改学生\n");
        printf("4. 查询学生\n");
        printf("5. 显示所有学生\n");
        printf("0. 返回主菜单\n");
        printf("请选择: ");
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                printf("输入学号: ");
                scanf("%s", id);
                printf("输入姓名: ");
                scanf("%s", name);
                if (add_student(id, name) == SUCCESS) {
                    printf("添加学生成功!\n");
                } else {
                    printf("添加学生失败!\n");
                }
                break;
            case 5:
                display_all_students();
                break;
            case 0:
                return;
            default:
                printf("无效选择!\n");
        }
    }
}

void course_management() {
    int choice;
    char id[20], name[50], type[30];
    int hours, max_students;
    float credit;
    
    while(1) {
        printf("\n--- 课程管理 ---\n");
        printf("1. 添加课程\n");
        printf("2. 删除课程\n");
        printf("3. 修改课程\n");
        printf("4. 查询课程\n");
        printf("5. 显示所有课程\n");
        printf("0. 返回主菜单\n");
        printf("请选择: ");
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                printf("输入课程号: ");
                scanf("%s", id);
                printf("输入课程名: ");
                scanf("%s", name);
                printf("输入课程类型: ");
                scanf("%s", type);
                printf("输入学时: ");
                scanf("%d", &hours);
                printf("输入学分: ");
                scanf("%f", &credit);
                printf("输入最大人数: ");
                scanf("%d", &max_students);
                if (add_course(id, name, type, hours, credit, max_students) == SUCCESS) {
                    printf("添加课程成功!\n");
                } else {
                    printf("添加课程失败!\n");
                }
                break;
            case 5:
                display_all_courses();
                break;
            case 0:
                return;
            default:
                printf("无效选择!\n");
        }
    }
}

void course_selection_management() {
    int choice;
    char student_id[20], course_id[20];
    
    while(1) {
        printf("\n--- 选课管理 ---\n");
        printf("1. 学生选课\n");
        printf("2. 学生退课\n");
        printf("3. 查看学生选课情况\n");
        printf("0. 返回主菜单\n");
        printf("请选择: ");
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                printf("输入学号: ");
                scanf("%s", student_id);
                printf("输入课程号: ");
                scanf("%s", course_id);
                if (student_select_course(student_id, course_id) == SUCCESS) {
                    printf("选课成功!\n");
                } else {
                    printf("选课失败! (可能学号/课程号不存在、已满或重复选课)\n");
                }
                break;
            case 2:
                printf("输入学号: ");
                scanf("%s", student_id);
                printf("输入课程号: ");
                scanf("%s", course_id);
                if (student_drop_course(student_id, course_id) == SUCCESS) {
                    printf("退课成功!\n");
                } else {
                    printf("退课失败! (可能学号/课程号不存在或未选该课程)\n");
                }
                break;
            case 3:
                display_student_courses();
                break;
            case 0:
                return;
            default:
                printf("无效选择!\n");
        }
    }
}

void display_student_courses() {
    printf("\n=== 学生选课详情 ===\n");
    if (student_count == 0) {
        printf("暂无学生信息\n");
        return;
    }
    
    for (int i = 0; i < student_count; i++) {
        printf("学号: %s, 姓名: %s, 已选课程数: %d\n",
               students[i].id, students[i].name, students[i].course_count);
        if (students[i].course_count > 0) {
            printf("  已选课程: ");
            for (int j = 0; j < students[i].course_count; j++) {
                printf("%s ", students[i].selected_courses[j]);
            }
            printf("\n");
        } else {
            printf("  暂无选课\n");
        }
    }
}

int main() {
    printf("=== 学生选课系统 ===\n");
    
    initialize_data(INPUT_FROM_FILE);

    while(1) {
        printf("\n=== 主菜单 ===\n");
        printf("1. 学生管理\n");
        printf("2. 课程管理\n");
        printf("3. 选课管理\n");
        printf("4. 显示所有信息\n");
        printf("0. 退出\n");
        printf("请选择: ");
        
        int choice;
        scanf("%d", &choice);
        
        switch(choice) {
            case 1:
                student_management();
                break;
            case 2:
                course_management();
                break;
            case 3:
                course_selection_management();
                break;
            case 4:
                display_all_students();
                display_all_courses();
                display_student_courses();
                break;
            case 0:
                save_students_to_file();
                save_courses_to_file();
                printf("数据已保存，再见!\n");
                return 0;
            default:
                printf("无效选择!\n");
        }
    }
    return 0;
}
